package com.situ.object.controll;


import com.situ.object.utils.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/*
* 上传图片
* */
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //上传
        //获取文件
        Part part = req.getPart("file");
        //项目路径
        String path = req.getServletContext().getRealPath("/upload/");
        System.out.println("***************");
        System.out.println("path:"+path);
        //上传
        String fileName = FileUploadUtil.upload(part,path);
        //返回
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write("{\"code\":0,\"src\":\"" + fileName + "\"}");

    }
}
